(function($) {
	var url = location.href.split("/");
	//var servicename = ['/splat-web-admin','ec-admin'];
	var baseUrl = "http://" + url[2] + "/" + url[3];
	var pageNo;
	var pageSize;
	$.fn.getCheckData = function(){
		var $this = $(this);
		var res = new Array();
		var trs = new Array();
		var $tbody = $this.find("tbody:eq(0)");
		var index = 0;
		$this.attr("indexflag") == "true"?index++:index;
		$this.attr("checkBox") == "true"?index++:index;
		$tbody.find("th").each(function(){
			res.push($(this).attr("name"));
		});
		$this.find("td input[type='checkbox']").each(function() {
			if($(this).is(':checked')) {
				var tr = {};
				var $td = $(this).parent().parent().find("td");
				for(var i = index,j = 0 ; i < res.length + index ; i++){
					if(i == index){
						tr[$this.attr("dataId")] = $($td[i]).find("input").val();
						$($td[i]).find("input").remove();
					}
					tr[res[j]] = $td[i].innerHTML;
					j++;
				}
				trs.push(tr);
			}
		});
		return trs;
	}
	$.fn.ECDATable = function() {
		var $this = $(this);
		var $ths = [];
		var id = $this.attr("id");
		var paramdata = {};
		var indexflag = $this.attr("indexflag") == "true";
		if(!pageNo){
			pageNo = 1;
		}
		paramdata["pageNo"] = pageNo;
		paramdata["pageSize"] = 5;
		var operate = $this.attr("operate") == "true";
		var cachehtml = $this.children("tbody[style='display:none;']").html();
		$this.empty();
		$this.next().remove();
		cachehtml = "<tbody style = 'display:none;'>" + cachehtml + "</tbody>";
		$this.append(cachehtml);
		var html = "<thead class=\"text-c\"><tr>";
	
	/*	if(indexflag){
			html += "<td width=\"25\" class=\"sorting_disabled\"  style=\"width: 63px;\">序号</td>";
		}*/
		$this.find("td").each(function(){
			var $th = {};
			$th.name = $(this).attr("name");
			$th.style = $(this).attr("style");
			$th.type = $(this).attr("type");
			$th.title = $(this).attr("title");
			$ths.push($th);
			html += "<td style = '" + $th.style + "'>" + $th.title + "</td>"; 
		});
		if(operate){
			html += "<td style = 'width:80px'>操作</td>";
		}
		html += "</tr></thead>";
		$this.prepend(html);
		var t = $("#" + $this.attr("bindForm")).serializeArray();
	    $.each(t, function() {
	    	paramdata[this.name] = this.value;
	    });
	    var dataAction = $this.attr("dataAction");
	    if(!dataAction){
	    	return;
	    }
	    
	   var type = $this.attr("type");
	   paramdata["pageSize"] = 5;
	    var url = baseUrl + dataAction;
	    url += "?pageNo="+pageNo+"&type="+type;
	    jQuery.ajax({
	        url: url,
	        dataType: 'json',
	        timeout: 60000,
	        cache: false,
	        success: function(page) {
	        	//body begin
	        	var tds = "<tbody class=\"text-c\">";
				pageSize = page.pageSize;
				pageNo = page.pageNo;
				pageCount = page.count%pageSize == 0?page.count/pageSize:(page.count/pageSize+1);
				var data = page.list;
				if(data){
					var length = data.length;
					for(var i = 0 ; i < length ; i++){
						tds += "<tr>"
						var size = $ths.length;

						/*if(indexflag){
							tds += "<td>" + (i + 1) + "</td>";
						}*/
						for(var j = 0 ; j < size ; j++){
							tds += "<td>";
							if(j == 0){
								tds += "<input type = 'hidden' value = '" + data[i][$this.attr("dataId")] + "' />"
							}
							var names = $ths[j].name.split(".");
							var value = names.length > 1 ?data[i][names[0]][names[1]]?data[i][names[0]][names[1]]:"":data[i][names[0]]?data[i][names[0]]:"";
							tds += value + "</td>";
						}
						/*if(operate){
							var id = data[i][$this.attr("dataId")];
							var detailHref = $this.attr("detailHref") + "?" + $this.attr("dataId") + "=" + id;
							var deleteHref = $this.attr("deleteHref") + "?" + $this.attr("dataId") + "=" + id;
							tds += "<td><i code="+data[i].id+"  class='btn-operation' onclick='updateCountry(this)'>修改</i><i code="+data[i].id+"  class='btn-operation1' onclick='deleteCountry(this)'>删除</i></td>";
						}*/
						if(operate){
							var operateLable = $this.attr("operateLable").split(",");
							var operateHref = $this.attr("operateHref").split(",");
							var operateName = $this.attr("operateName");
							if(operateLable.length != operateHref.length){
								layer.msg("配置错误");
								continue;
							}
							tds += "<td>";
							for(var i = 0 ; i < operateLable.length ; i++){
								tds += "<a href = '" + operateHref[i] + data[i][operateName] + "'>" + operateLable[i] + "</a>";
								if(i < operateLable.length - 1){
									tds += "&nbsp;&nbsp;|&nbsp;&nbsp;";
								}
							}
							tds += "</td>";
						}
						tds += "</tr>";
					}
				}
				tds += "</tbody>";
				$this.append(tds);
				//body end
				//Page begin

				if($this.attr("pagenation") != "false"){
					$this.parent().append(page.html);
				}
			    $this.next().find("a").click(function(){
			    	var dataPage = $(this).attr("data-page");
			    	var text = $(this).text();
			    	if(!isNaN(dataPage)){
			    		pageNo = $(this).attr("data-page");
			    	}else if(pageNo>1&&dataPage == "pre"){
			    		pageNo--
			    	}else if(pageNo<pageCount&&dataPage == "next"){
			    		pageNo++;
			    	}else if(text == "跳转"){
			    		dataPage = $(this).prev().val();
			    		if(1 <= dataPage <= pageCount){
			    			pageNo = dataPage;
			    		}else{
			    			return;
			    		}
			    	}else{
			    		return;
			    	}
			    	$("#" + $this.attr("bindClick")).click();
			    });
			    //Page end
			    //operate begin
			    $this.find("td a").click(function (){
			    	var $a = $(this);
			    	var dataHref = $a.attr("data-href");
			    	var html = !dataHref.indexOf(".html") > -1;
			    	if(html){
						jQuery.ajax({
							url: baseUrl + dataHref,							
							dataType: 'json',
							timeout: 60000,
							cache: false,
							success: function(data) {
								if(data.success){
								layer.msg("删除成功");
									
									$("#" + $this.attr("bindClick")).click();
								}
						    }
						});
			    	}
			    });
			    //operate end
			    //checkbox begin
			    $this.find("th input[type='checkbox']").click(function(){
			    	$this.find("td input[type='checkbox']").prop("checked",$(this).is(":checked"));
			    });
	        }
	    });
	};  
})(jQuery);

function getDictValue(status,type){
	var data = {};
	data.values = status;
	data.type = type;
	data.defaultValue='';
	jQuery.ajax({
        url: baseUrl+'/dict/getDictLabels',
        timeout: 60000,
        cache: false,
        data:data,
        success: function(datas) {
        return datas;
        }
     })
}

$(document).ready(function() {
	var $table = $('table[datatable="true"]');
	$table.each(function(){
		var table = $(this).ECDATable();
		var $tableClick = $("#" + $(this).attr("bindClick"));
		$tableClick.click(function(){
			$('table[datatable="true"][bindClick="'+ $(this).attr("id") +'"]').ECDATable();
		});
	})
	
	
})





